<?php
/**
 * File for holding the cImage-classe
 *
 * @author Xander Hoogland <home@xanland.nl>
 * @package Classes
 */

/**
 * Class for getting EXIF info from an image, but also resizing
 * images used.
 *
 * @author Xander Hoogland <home@xanland.nl>
 * @package Classes
 */
class cImage
{
    /**
     * All the info of an image that is available
     *
     * @access private
     * @var string
     */
    private $aImageInfo;

    /**
     * The full path to an image including the image itself
     *
     * @access private
     * @var array
     */
    private $sPathToImage;

    /**
     * The name and extension of an image
     *
     * @access private
     * @var string
     */
    private $sImage;

    /**
     * Full path to image without the image itself
     * @var string
     */
    private $sPath;

    /**
     * Constructor for settings vars and getting image info
     *
     * @access public
     * @param string $sPath
     * @param string $sImage
     */
    public function __construct ($sPath, $sImage)
    {
		$this -> sPathToImage = $sPath . $sImage;
		$this -> aImageInfo = getimagesize ($this -> sPathToImage);
		$this -> sPath = $sPath;
		$this -> sImage = $sImage;
    }

    /**
     * Getting the width of an image
     *
     * @access public
     * @return int
     */
    public function getWidth ()
    {
		return $this -> aImageInfo [0];
    }

    /**
     * Getting the height
     *
     * @access public
     * @return int
     */
    public function getHeight ()
    {
		return $this -> aImageInfo [1];
	}

    /**
     * Getting the the mimetype
     *
     * @access public
     * @return string
     */
    public function getMimetype ()
    {
		return $this -> aImageInfo ['mime'];
    }

    /**
     * The method for resizing an image accordingly to the ratio. It also
     * converts from and to the good image (mimetype).
     * @param int $iSize
     */
    public function resizeImage ($iSize) //resize=1000,thumb=200
    {
		$fRatio = $this -> getWidth () / $this -> getHeight ();

		$iWidth = $iSize;
		$iHeight = $iSize;

		$iWidth = $iHeight * $fRatio;

		$image_p = imagecreatetruecolor ($iWidth, $iHeight);

		if ($this -> getMimetype () == 'image/gif')
		{
			$image = imagecreatefromgif ($this -> sPathToImage);
		}
		elseif ($this -> getMimetype () == 'image/jpg' || $this -> getMimetype () == 'image/jpeg')
		{
			$image = imagecreatefromjpeg ($this -> sPathToImage);
		}
		elseif ($this -> getMimetype () == 'image/png')
		{
			$image = imagecreatefrompng ($this -> sPathToImage);
		}

		imagecopyresampled ($image_p, $image, 0, 0, 0, 0, $iWidth, $iHeight, $this -> getWidth (), $this -> getHeight ());

		if ($iSize == 200)
		{
			$sThumb = 'thumbs/';
		}
		else
		{
			$sThumb = '';
		}

		$this -> sImage = str_replace ('___', '', $this -> sImage);

		if ($this -> getMimetype () == 'image/gif')
		{
			imagegif ($image_p, $this -> sPath . $sThumb . $this -> sImage);
		}
		elseif ($this -> getMimetype () == 'image/jpg' || $this -> getMimetype () == 'image/jpeg')
		{
			imagejpeg ($image_p, $this -> sPath . $sThumb . $this -> sImage, 100);
		}
		elseif ($this -> getMimetype () == 'image/png')
		{
			imagepng ($image_p, $this -> sPath . $sThumb . $this -> sImage, 0);
		}

		imagedestroy ($image);
    }
}